using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
    #region using
    using System.Web.Mvc;
    using TemplateWeb.Models.ViewModels;
    using TemplateWeb.Models;
    using TemplateWeb.Models.Repositories;
    using TemplateWeb.Helpers;
    using System.Web.Security;
    using System.Globalization;
    #endregion
    public partial class AdminController : BaseController
    {
        public ActionResult SelectDienThoai()
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DIENTHOAI
            };
            return View(CreateViewModel(data));
        }


        public ActionResult SelectDienThoaiByIDNhaSanXuat(int idnhasanxuat)
        {
            var data = new DataTransferViewModel
            {
                IDNhaSanXuat = idnhasanxuat,
                EnumViewModelType = EnumViewModel.ADMIN_DIENTHOAI,
            };
            return View("SelectDienThoai", CreateViewModel(data));
        }

        public ActionResult SelectDienThoaiByIDNhaSanXuatPaging(int idnhasanxuat, int page = 1)
        {
            var data = new DataTransferViewModel
            {
                IDNhaSanXuat = idnhasanxuat,
                CurrentPage = page,
                EnumViewModelType = EnumViewModel.ADMIN_DIENTHOAI,
            };
            return PartialView("Templates/TH_ListDienThoai", CreateViewModel(data));
        }


        [AcceptVerbs(HttpVerbs.Delete)]
        public JsonResult DeleteDienThoai(int id)
        {
            // Delete sach truoc
            var result = _repDienThoai.Delete(id);
            if (result)
            {
                var spItem = _repSanPham.SelectByID(id);
                var fileName = spItem.HinhMinhHoa;
                result = _repSanPham.Delete(id);
                if (result)
                {
                    if (fileName != "noimages.jpg")
                    {
                        var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                        DeleteFile(fullPath);
                    }
                }
            }

            return Json(new
            {
                Success = result,
                RecordCount = _repDienThoai.SelectAll().Count,
                DeleteId = id
            });
        }

        public ActionResult DetailOfDienThoai(int id)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_DIENTHOAI,
                IntID = id
            };
            return View(CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateDienThoai(FormCollection forms)
        {
            string refName = string.Empty;
            long gia;
            int soluongton;

            string id = forms.GetValues("DienThoai_ID")[0];
            string idnhasanxuat = forms.GetValues("DienThoai_IDNhaSanXuat")[0];
            string tensanpham = forms.GetValues("DienThoai_TenSanPham")[0];
            string mota = forms.GetValues("DienThoai_MoTa")[0];
            string baohanh = forms.GetValues("DienThoai_BaoHanh")[0];
            string hangmuc = forms.GetValues("DienThoai_HangMuc")[0];
            var file = Request.Files["DienThoai_HinhMinhHoa"];

            // Update DienThoai
            var sachItem = _repDienThoai.SelectByID(int.Parse(id));

            sachItem.IDNhaSanXuat = int.Parse(idnhasanxuat.Replace(".", string.Empty).Replace(",", string.Empty));
            sachItem.HangMuc = hangmuc;

            var result = _repDienThoai.Save();

            // Update tiep san pham
            if (result)
            {
                var spItem = _repSanPham.SelectByID(int.Parse(id));

                if (!int.TryParse(forms.GetValues("DienThoai_SoLuongTon")[0].Replace(",", string.Empty).Replace(".", string.Empty), out soluongton))
                {
                    spItem.SoLuongTon = soluongton;
                }

                if (!long.TryParse(forms.GetValues("DienThoai_Gia")[0].Replace(",", string.Empty).Replace(".", string.Empty), out gia))
                {
                    spItem.GiaBan = gia;
                }

                spItem.TenSanPham = tensanpham;
                spItem.MoTa = mota;
                spItem.BaoHanh = baohanh;

                if (file.ContentLength != 0)
                {
                    result = UploadFile(file, "/Content/Images/Items/", ref refName);
                    spItem.HinhMinhHoa = refName;
                }

                result = _repSanPham.Save();
            }

            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_DIENTHOAI,
                IntID = int.Parse(id.Replace(".", string.Empty).Replace(",", string.Empty)),
                InfoText = result ? "Item has been updated" : "Cannot update this item"
            };
            return View("DetailOfDienThoai", CreateViewModel(data));
        }


        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertDienThoai(FormCollection forms)
        {
            long gia;
            int soluongton;
            string refName = string.Empty;
            var result = false;

            string idnhasanxuat = forms.GetValues("DienThoai_IDNhaSanXuat")[0];
            string tensanpham = forms.GetValues("DienThoai_TenSanPham")[0];
            string mota = forms.GetValues("DienThoai_MoTa")[0];
            string baohanh = forms.GetValues("DienThoai_BaoHanh")[0];
            string hangmuc = forms.GetValues("DienThoai_HangMuc")[0];
            var file = Request.Files["DienThoai_HinhMinhHoa"];

            // Insert san pham truoc
            var spItem = new SanPham();

            if (int.TryParse(forms.GetValues("DienThoai_SoLuongTon")[0].Replace(",", string.Empty).Replace(".", string.Empty), out soluongton))
            {
                spItem.SoLuongTon = soluongton;
            }

            if (long.TryParse(forms.GetValues("DienThoai_Gia")[0].Replace(",", string.Empty).Replace(".", string.Empty), out gia))
            {
                spItem.GiaBan = gia;
            }

            spItem.LoaiSanPham = StringEnum.GetStringValue(LoaiSanPham.DIEN_THOAI);
            spItem.TenSanPham = tensanpham;
            spItem.MoTa = mota;
            spItem.BaoHanh = baohanh;

            if (file.ContentLength != 0)
            {
                if (UploadFile(file, "/Content/Images/Items/", ref refName))
                {
                    spItem.HinhMinhHoa = refName;
                }
            }

            result = _repSanPham.Insert(spItem);

            // Neu insert san pham thanh cong, thi moi insert tiep sach
            if (result)
            {
                // Insert DienThoai
                var sachItem = new DienThoai
                {
                    ID = spItem.ID,
                    IDNhaSanXuat = int.Parse(idnhasanxuat),
                    HangMuc = hangmuc
                };
                result = _repDienThoai.Insert(sachItem);
            }

            var data = new DataTransferViewModel
            {
                IDNhaSanXuat = int.Parse(idnhasanxuat),
                EnumViewModelType = EnumViewModel.ADMIN_DIENTHOAI,
                InfoText = result ? "New item has been added" : "Cannot insert new item"
            };
            return View("SelectDienThoai", CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimKiemDienThoai(FormCollection forms)
        {
            var tensanpham = forms.GetValues("TimKiemTenSanPham")[0];
            var nhasanxuat = forms.GetValues("TimKiemNhaSanXuat")[0];

            ViewData["TuKhoa"] = tensanpham;

            var data = new DataTransferViewModel
            {
                TimKiem_TenSanPham = tensanpham,
                IDNhaSanXuat = int.Parse(nhasanxuat),
                EnumViewModelType = EnumViewModel.ADMIN_TIMKIEM_DIENTHOAI,
            };
            return View("SelectDienThoai", CreateViewModel(data));
        }
    }
}